package net.vkits;

import net.vkits.anjiamiao.application.CustomerApplicationService;
import net.vkits.anjiamiao.application.VerifyCodeApplicationService;
import net.vkits.common.dto.CustomerDTO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

import static org.junit.Assert.assertEquals;

/**
 * Created by ethan-liu on 16/3/31.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BackendApplication.class)
@ActiveProfiles("dev")
@WebAppConfiguration
public class addTestUser {
    private static final Logger logger = LoggerFactory.getLogger(addTestUser.class);

    @Test
    public void test() {
        //Do nothing
    }
//    @Resource
//    private UserRepository userRepository;
//
//    @Resource
//    private UserApplicationService userApplicationService;
//
//    @Test
//    public void createUserAndPermission() {
//        final User user = new User("test","123456");
//        userRepository.save(user);
//    }
//
//    @Test
//    public void setAdminUser() {
//        userApplicationService.setAdminUser("ethan");
//    }

    @Resource
    private VerifyCodeApplicationService verifyCodeApplicationService;

    @Resource
    private CustomerApplicationService customerApplicationService;

    @Test
    public void smsTest() throws Exception {
        verifyCodeApplicationService.sendSmsCode("15901133971");
    }

//    @Test
//    public void codeTest() {
//        CustomerDTO customerDTO = new CustomerDTO();
//        customerDTO.setName("cuicui");
//        customerDTO.setCellPhoneNum("15901133971");
//        customerDTO.setVerifyCode("473274");
//        customerApplicationService.login(customerDTO);
//    }

    @Test
    public void cellPhoneNumFormatTest() {
        CustomerDTO customerDTO = new CustomerDTO();
        customerDTO.setName("cuicui");
        customerDTO.setCellPhoneNum("17702392954");
        customerDTO.setVerifyCode("473274");
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<CustomerDTO>> violations = validator.validate(customerDTO);

        logger.info("Size is: {}", violations.size());
        logger.info("Message is: {}", violations.iterator().next().getMessage());

        assertEquals(1, violations.size());
        assertEquals("错误的手机号码格式", violations.iterator().next().getMessage());
    }

    @Test
    public void idCardNoFormatTest() {
        CustomerDTO customerDTO = new CustomerDTO();
        customerDTO.setName("cuicui");
        customerDTO.setCellPhoneNum("15202392954");
        customerDTO.setVerifyCode("473274");
        customerDTO.setIdCardNo("000200003034");
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<CustomerDTO>> violations = validator.validate(customerDTO);

        logger.info("Size is: {}", violations.size());
        logger.info("Message is: {}", violations.iterator().next().getMessage());

        assertEquals(1, violations.size());
        assertEquals("错误的身份证号码格式", violations.iterator().next().getMessage());
    }
}
